<?php
namespace Admin\Controller;

class NodeController extends AdminController
{
    public function index()
    {
        $this->display();
    }

    public function view()
    {
        //Load('extend');
        $n = D("Node");
        import("ORG.Util.Page");
        //导入分页类
        $page = empty($_POST["page"]) ? 1 : $_POST["page"];
        $rows = empty(I("limit")) ? 1 : I("limit");
        $sort = empty($_POST["sort"]) ? 'id' : $_POST["sort"];
        $order = empty($_POST["order"]) ? 'desc' : $_POST["order"];
        //dump($order);
        if ($_POST["account"] != null && $_POST["account"] != "") {
            $condition['account'] = array('like', '%' . $_POST["account"] . '%');
        }
        if ($_POST["nickname"] != null && $_POST["nickname"] != "") {
            $condition['nickname'] = array('like', '%' . $_POST["nickname"] . '%');
        }
        if ($_POST["createdatetimeStart"] != null && $_POST["createdatetimeStart"] != "") {
            $condition['create_time'] = array('between', $_POST["createdatetimeStart"], $_POST["createdatetimeEnd"]);
        }
        if ($_POST["modifydatetimeStart"] != null && $_POST["modifydatetimeStart"] != "") {
            $condition['update_time'] = array('between', $_POST["modifydatetimeStart"], $_POST["modifydatetimeEnd"]);
        }
        $count = $n->where($condition)->count();
        //计算总数
        //$p = new Page($count,$rows);order($sort+','+$order)->

        $list = $n->relation(false)->where($condition)->order(array($sort => $order))->select();
        // dump($list);

        // import('ORG.Util.Tree');
        $list = \Org\Util\Tree::toLevel($list, '|——', 0);
        $list = json_encode($list);
        $result = '{"status":1,"msg":"成功啦","total":' . $count . ',"rows":' . $list . '}';
        echo ($result);
    }
    //添加提交处理
    public function add()
    {
        if (empty($_POST)) {
            $nodelist = M('Node')->order('id desc')->select();
            $nodelist = \Org\Util\Tree::toLevel($nodelist, '|——', 0);
            $this->assign('nodelist', $nodelist);
            $this->display();
        } else {
            $m = D("Node");
            if (!$m->create()) {
                $this->error($m->getError(), 3);
            } else {
                if ($result = $m->add()) {
                    $this->success('添加成功！', 1);
                } else {
                    $this->error('添加失败！', 0);
                }
            }
        }
    }
    // 删除数据
    public function delete()
    {
        if (!empty($_GET['ids'])) {
            $d = D("node");
            $condition['id'] = array('in', $_GET['ids']);
            $result = $d->where($condition)->delete();
            if (false !== $result) {
                $this->success('删除成功！', 1);
            } else {
                $this->error('删除出错！', 1);
            }
        } else {
            $this->error('删除项不存在！', 1);
        }
        //$this->redirect('index');
    }
    // 更新数据
    public function edit()
    {
        if (empty($_POST)) {
            $nodelist = M('Node')->order('id desc')->select();
            $nodelist = \Org\Util\Tree::toLevel($nodelist, '|——', 0);
            $this->assign('nodelist', $nodelist);
            $pid = M('Node')->where('id=' . $_GET['id'])->getField('pid');
            $this->assign('pid', $pid);
            $this->display();
        } else {
            $Form = D("Node");
            //$_POST['pid'] = !empty($_POST['pid']) ? $_POST['pid'] : 0;
            if ($vo = $Form->create()) {
                $list = $Form->save($vo);
                if ($list !== false) {
                    $this->success('更新成功！', 1);
                } else {
                    $this->error('没有更新任何数据!', 0);
                }
            } else {
                $this->error($Form->getError(), 3);
            }
        }
    }
    //用户公共信息组件
    public function viewWidget()
    {
        $n = D("Node");
        $list = $n->select();
        $list = json_encode($list);
        echo ($list);
        //输出json数据
    }

}
